package Windows;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.tree.DefaultTreeModel;

import org.json.JSONObject;

import Bean.Msg;
import MainThread.MyClient;
import Utils.DateBaseUpdateUtils;
import Utils.SendMsgUtils;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;

public class FriendTree extends JFrame {

	/**
	 * @author etcxy
	 */
	// 该线程负责处理的Socket
	private Socket s;
	private JPanel contentPane;
	private JTree tree;
	private String mySelf = null;

	// 在线好友列表
	public static ArrayList<String> friendLine = null;

	// 打开的对话框
	public static Map<String, iTalk> friendiTalk = null;

	// 打开的树节点
	public Map<String, DefaultMutableTreeNode> treeNode = null;

	private DefaultMutableTreeNode node1;
	private DefaultMutableTreeNode node2;
	private DefaultMutableTreeNode node3;
	private JPanel panel_1;
	private JTextField addFriends;
	private JButton btn_add;

	/**
	 * Launch the application.
	 */
	// public static void main(String[] args) {
	// EventQueue.invokeLater(new Runnable() {
	// public void run() {
	// try {
	// FriendTree frame = new FriendTree(mySelf);
	// frame.setVisible(true);
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }
	// });
	// }

	/**
	 * Create the frame.
	 */
	public FriendTree(Socket s, String mySelf) {
		this.mySelf = mySelf;
		this.s = s;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 318, 660);

		friendiTalk = new HashMap<>();
		treeNode = new HashMap<>();
		friendLine = new ArrayList<>();

		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		panel.setLayout(new BorderLayout(0, 0));

		DefaultMutableTreeNode top = new DefaultMutableTreeNode("聊天列表");
		node1 = new DefaultMutableTreeNode("在线好友");
		node2 = new DefaultMutableTreeNode("离线好友");
		node3 = new DefaultMutableTreeNode("最近聊天");

		top.add(node1);
		top.add(node2);
		top.add(node3);

		tree = new JTree(top);
		panel.add(tree);

		panel_1 = new JPanel();
		panel.add(panel_1, BorderLayout.SOUTH);
		panel_1.setLayout(new BorderLayout(0, 0));

		addFriends = new JTextField();
		panel_1.add(addFriends, BorderLayout.CENTER);
		addFriends.setColumns(10);

		btn_add = new JButton("好友添加");
		btn_add.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Msg msg = new Msg();
				msg.MsgInfo(mySelf, addFriends.getText(), MyClient.ADD, "1");
				SendMsgUtils.sendMsg(s, new JSONObject(msg).toString());
			}
		});

		panel_1.add(btn_add, BorderLayout.EAST);

		tree.addMouseListener(new MouseListener() {
			@Override
			public void mouseReleased(MouseEvent e) {

			}

			@Override
			public void mousePressed(MouseEvent e) {

			}

			@Override
			public void mouseExited(MouseEvent e) {

			}

			@Override
			public void mouseEntered(MouseEvent e) {

			}

			@Override
			public void mouseClicked(MouseEvent e) {
				DefaultMutableTreeNode selectionNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
				if (e.getClickCount() == 2) { // 鼠标双击事件
					try {
						if (selectionNode.isLeaf()) { // 叶子节点的监听
							String tofriend = selectionNode.toString();
							if (friendLine.contains(tofriend)) {
								System.out.println(selectionNode.toString() + "被点击");
								iTalk italk = new iTalk(s, tofriend, mySelf);
								italk.setVisible(true);
								friendiTalk.put(tofriend, italk);
							}
						}
					} catch (Exception ex) {
					}
				}
			}
		});

		// 设置使用Windows风格外观
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (Exception e1) {
			e1.printStackTrace();
		}

		DefaultTreeCellRenderer cellRender = new DefaultTreeCellRenderer();
		cellRender.setOpenIcon(new ImageIcon("icon/cat1.png"));
		cellRender.setLeafIcon(new ImageIcon("icon/cat.png"));
		tree.setCellRenderer(cellRender);

		// 更新JTree的UI外观
		SwingUtilities.updateComponentTreeUI(tree);

		// 请求好友列表
		Msg msg = new Msg();
		msg.setFrom(mySelf);
		msg.setTo("server");
		msg.setLogin_reg_chat_ack(MyClient.FRIEND);

		SendMsgUtils.sendMsg(s, new JSONObject(msg).toString());
	}

	/*
	 * 刷新聊天窗口主页面
	 */
	public void flushUI(String content) {

		String[] friendLAO = content.split(":");
		if (friendLAO[0].equals("line")) {
			friendLine.add(friendLAO[1]);
			treeNode.put(friendLAO[1], new DefaultMutableTreeNode(friendLAO[1]));
			node1.add(treeNode.get(friendLAO[1]));
		} else if (friendLAO[0].equals("offline")) {
			treeNode.put(friendLAO[1], new DefaultMutableTreeNode(friendLAO[1]));
			node2.add(treeNode.get(friendLAO[1]));
		}
	}

	public void newFriend(String friend) {
		friendLine.add(friend);
		treeNode.put(friend, new DefaultMutableTreeNode(friend));
		node1.add(treeNode.get(friend));
		tree.updateUI();
	}

	public void FriendUAD(boolean x, String friend,boolean y) {
		if(y){
			newFriend(friend);
			return;
		}
		if (x) {
			friendLine.add(friend);
			// if (treeNode.containsKey(friend)) {
			node2.remove(treeNode.get(friend));
			// }
			node1.add(treeNode.get(friend));

		} else {
			friendLine.remove(friend);
			// if (treeNode.containsKey(friend)) {
			node1.remove(treeNode.get(friend));
			// }
			node2.add(treeNode.get(friend));
		}
		tree.updateUI();
	}

	public void treeUpdateUI() {
		tree.updateUI();
	}

}
